# -*- coding: utf-8 -*-
import json
import logging
import requests
import datetime
from ronglian_sms_sdk import SmsSDK

accId = '8a216da8580*********1cf3f01070'
accToken = 'be47d4dc*********929ca4f8fbe3'
appId = '8a216da85*********44a1076'

def handler(event, context):
    logger = logging.getLogger()
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,"
                  "application/signed-exchange;v=b3;q=0.9",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
        "Cache-Control": "max-age=0",
        "Connection": "keep-alive",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"
                      "89.0.4389.90 Safari/537.36"
    }

    res = "无结果";
    url='https://data.hexin.cn/ipo/bond/cate/info/' #需要爬数据的网址
    page=requests.get(url, headers=headers).text
    json_array = json.loads(page)
    for item in json_array:
        sgDate = item['sgDate']
        zqName = item['zqName']
        disRate = item['disRate']
        n_time=datetime.datetime.now().strftime('%Y-%m-%d')
        if(sgDate == n_time):
            res = "[zqName:"+zqName+'\tdisRate:'+disRate+']'
            sdk = SmsSDK(accId, accToken, appId)
            tid = '227049'
            mobile = '17600805308'
            datas = (zqName, disRate+'%')
            resp = sdk.sendMessage(tid, mobile, datas)
            logger.info(resp)
            logger.info("发送提醒：", datas)
        else:
            logger.info(zqName+'无需发送：'+sgDate)
    return res


if __name__ == '__main__':
    handler('null','null')